<?php
/**
 * Created by PhpStorm.
 * User: 宋春宝
 * Date: 2018/11/5
 * Time: 18:06
 */

namespace app\wchat\controller;


use think\Request;

class Power extends Common
{
    public function index()
    {
        return $this->fetch('list');
    }

    /**
     * 跟踪页面
     * @return mixed
     */
    public function trackUI(){
        return $this->fetch('enter/track');
    }

    /**
     * 派单任务列表
     * @param Request $request
     * @return array
     */
    public function queryDis(Request $request){
        $input = $request->param();
        $input['gid'] = session('mgid');
        $input['dis'] = [0];
        $at = db('power')->where('admin_id',session('muid'))->select();
        foreach ($at as $v){
            if($v['forever'] == 1){
                $input['dis'][] = $v['dispatch_id'];
            }else{
                if(strtotime($v['expire'])>=strtotime(date('Y-m-d'))){
                    $input['dis'][] = $v['dispatch_id'];
                }
            }
        }
        $rows = db('dispatch')->alias('w')->order('w.id desc')->where(function ($query)use($input){

            $query->where('w.id','in',$input['dis']);
            if(!empty($input['fault_id'])){
                $query->where('w.fault_id',$input['fault_id']);
            }
            if(!empty($input['state_id'])){
                $sis = explode(',',$input['state_id']);
                $query->where('w.state_id','in',$sis);
            }

            if(!empty($input['key'])){
                $query->where('w.title|ws.number|w.tel','LIKE','%'.$input['key'].'%');
            }
        })
            ->join('state st','st.id=w.state_id','left')
            ->join('fault ft','ft.id=w.fault_id','left')
            ->join('works ws','ws.id=w.works_id','left')
            ->join('admin a','a.admin_id=w.admin_id','left')
            ->field(['w.*','st.state_name','ft.title as fault_name','ws.attach as w_attach','ws.check_code','a.username'])
            ->paginate($input['limit'])
            ->toArray();
        return ['code'=>1,'msg'=>'获取成功!','data'=>$rows['data'],'count'=>$rows['total'],'rel'=>1];
    }

    /**
     * 详情页面
     * @return mixed
     */
    public function getDisUI(){

        $id = input('id');

        if(empty($id)){
            $row = db('dispatch')->where('works_id',input('wid'))->field(['id'])->find();
            $id = $row['id'];
        }
        $info = db('dispatch')->alias('w')->where('w.id',$id)
            ->join('state st','st.id=w.state_id','left')
            ->join('fault ft','ft.id=w.fault_id','left')
            ->join('works ws','ws.id=w.works_id','left')
            ->join('admin a','a.admin_id=w.admin_id','left')
            ->join('admin reg','reg.admin_id=w.reg_id','left')
            ->field(['w.*','st.state_name','ft.title as fault_name','ws.attach as w_attach','ws.check_code','a.username','reg.username as reg_username','reg.name as reg_name'])
            ->find();
        $at = db('dispatch_at')->where(['dispatch_id'=>$id,'admin_id'=>session('muid')])->find();

        $info['is_conf'] = $at['is_conf'];
        $info['cause'] = $at['cause'];
        if(!$at) $info['none'] = 1;
        else $info['none'] = 0;

        return $this->fetch('getDis',['info'=>$info]);
    }

    /**
     * 跟踪列表
     * @param Request $request
     */
    public function track(Request $request){
        $did = $request->param('dis_id');
        if(empty($did)) return ['code'=>0,'msg'=>'dis_id参数错误'];

        $rows = db('dispatch_flow')->alias('df')
            ->order('df.id desc')
            ->where('df.dispatch_id',$did)
            ->join('admin a','a.admin_id=df.admin_id','left')
            ->field(['df.*','a.username'])
            ->select();
        return ['code'=>1,'data'=>$rows];
    }
}